﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace SearchHighlight
{
    public partial class Form1 : Form
    {
        int start = 0;
        int indexOfSearchText = 0;
        //int startindex = -1;
        public Form1()
        {
            InitializeComponent();
            //LockWindowUpdate(this.Handle);
            //button1_Click(null, null);
            //this.Validate();
            //this.Show();
            //rtb.Show();
            this.rtb.LoadFile(@"c:\temp\original-title2.rtf");
            this.txtSearch.Text = "北京"; //"Glucosamine";
            find();
            display.Rtf = rtb.Rtf; 
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            start = 0;
            indexOfSearchText = 0;
        }

        public void button1_Click(object sender, EventArgs e)
        {
           
            //LockWindowUpdate(this.Handle);

            find();

            //LockWindowUpdate(IntPtr.Zero);

      
            display.Rtf = rtb.Rtf; 
        }

        private void find()
        {
            int startindex = 0;

            if (rtb.Text.Length > 0)
                startindex = FindMyText(this.txtSearch.Text.Trim(), start, rtb.Text.Length);

            // If string was found in the RichTextBox, highlight it
            if (startindex >= 0)
            {
                // Set the highlight color as red
                rtb.SelectionColor = Color.Red;
                // Find the end index. End Index = number of characters in textbox
                int endindex = txtSearch.Text.Length;
                // Highlight the search string
                rtb.SelectionStart = startindex;
                rtb.SelectionLength = txtSearch.Text.Length;
               
                rtb.Select(startindex, endindex);
                rtb.SelectedText = txtSearch.Text;
                // mark the start position after the position of 
                // last search string
                start = startindex + endindex;
                //rtb.Update();
                //MessageBox.Show("ok");
                find();
            }

        }
        public int FindMyText(string txtToSearch, int searchStart, int searchEnd)
        {
            // Unselect the previously searched string
            if (searchStart > 0 && searchEnd > 0 && indexOfSearchText >= 0)
            {
                rtb.Undo();
            }

            // Set the return value to -1 by default.
            int retVal = -1;

            // A valid starting index should be specified.
            // if indexOfSearchText = -1, the end of search
            if (searchStart >= 0 && indexOfSearchText >= 0)
            {
                // A valid ending index 
                if (searchEnd > searchStart || searchEnd == -1)
                {
                    // Find the position of search string in RichTextBox
                    indexOfSearchText = rtb.Find(txtToSearch, searchStart, searchEnd, RichTextBoxFinds.None);
                    // Determine whether the text was found in richTextBox1.
                    if (indexOfSearchText != -1)
                    {
                        // Return the index to the specified search text.
                        retVal = indexOfSearchText;
                    }
                }
            }
            return retVal;
        }

        private void rtb_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            start = 0;
            indexOfSearchText = 0;
        }

        [DllImport("user32.dll")]
        public static extern bool LockWindowUpdate(IntPtr hWndLock);

        private const int WM_SETREDRAW      = 0x000B;
private const int WM_USER           = 0x400;
private const int EM_GETEVENTMASK   = (WM_USER + 59);
private const int EM_SETEVENTMASK   = (WM_USER + 69);
 
[DllImport("user32", CharSet = CharSet.Auto)]
private extern static IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, IntPtr lParam);
 
IntPtr eventMask = IntPtr.Zero;

private void Form1_Load(object sender, EventArgs e)
{

}
/*try
{
      // Stop redrawing:
      SendMessage(richTextBox1.Handle, WM_SETREDRAW, 0, IntPtr.Zero);
      // Stop sending of events:
      eventMask = SendMessage(richTextBox1.Handle, EM_GETEVENTMASK, 0, IntPtr.Zero);
 
      // change colors and stuff in the RichTextBox
}
finally
{
      // turn on events
      SendMessage(richTextBox1.Handle, EM_SETEVENTMASK, 0, eventMask);
      // turn on redrawing
      SendMessage(richTextBox1.Handle, WM_SETREDRAW, 1, IntPtr.Zero);
}*/

    }
}
